package com.homedo.odin.solution.controller.pc;

import com.homedo.core.common.base.controller.BaseController;
import com.homedo.core.common.base.dto.Meta;
import com.homedo.core.common.base.dto.ResultData;
import com.homedo.core.common.exception.BizException;
import com.homedo.core.common.util.NumberHelper;
import com.homedo.odin.solution.contract.pc.UserCommentAddPcRequestBean;
import com.homedo.odin.solution.contract.pc.UserCommentListPCResponseBean;
import com.homedo.odin.solution.enumcode.basic.ColumnTypeEnum;
import com.homedo.odin.solution.service.bizservice.pc.UserCommentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import javax.validation.Valid;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: ZhangLei
 * @date: 2018/4/9
 * @Description:
 */
@Api(description = "评论接口", tags = {"Comment"}, produces = MediaType.APPLICATION_JSON_VALUE)
@RestController
public class UserCommentController extends BaseController<UserCommentService> {

    @PostMapping("/comments")
    @ApiOperation(value = "PC_用户添加评论", httpMethod = "POST")
    public ResultData addUserComment(@Valid @RequestBody UserCommentAddPcRequestBean requestBean,
            BindingResult bindingResult) throws Exception {
        if (bindingResult.hasErrors()) {
            String defaultMessage = bindingResult.getFieldError().getDefaultMessage();
            return ResultData.newInstance(Meta.CODE_451, defaultMessage);
        }
        if (!ColumnTypeEnum.dbValueExist(requestBean.getType())) {
            throw new BizException(Meta.CODE_451.getCode(), "PC评论类型参数不正确");
        }
        Long commentId = baseService.pcAddUserComment(requestBean);
        return ResultData.newInstance(Meta.CODE_200, commentId);
    }

    @GetMapping("/comments")
    @ApiOperation(value = "PC_获取[栏目]的评论信息列表", notes = "PC_获取[栏目]的评论信息列表", httpMethod = "GET", response = UserCommentListPCResponseBean.class)
    public ResultData queryUserCommentList(
            @ApiParam(value = "当前登录用户", required = false) @RequestParam(value = "uid", required = false) Integer userId,
            @ApiParam(value = "栏目id", required = true) @RequestParam("columnId") Long columnId,
            @ApiParam(value = "当前页码", required = true) @RequestParam("pageNum") Integer currentPage,
            @ApiParam(value = "每页记录数", required = true) @RequestParam("pageSize") Integer pageSize) {
        if (!NumberHelper.validate(columnId) || !NumberHelper.validate(currentPage) ||
                !NumberHelper.validate(pageSize)) {
            throw new BizException(Meta.CODE_451.getCode(), "请求参数不合法");
        }
        if (userId != null) {
            if (!NumberHelper.validate(userId)) {
                throw new BizException(Meta.CODE_451.getCode(), "userId不合法");
            }
        }

        UserCommentListPCResponseBean responseBean = baseService
                .queryUserCommentListByColumnId(columnId, userId, currentPage, pageSize);
        return ResultData.newInstance(Meta.CODE_200, responseBean);
    }

    @PostMapping(value = "/likes/comments")
    @ApiOperation(value = "PC_点赞评论", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "form", dataType = "Long", name = "commentId", value = "评论id", required = true),
            @ApiImplicitParam(paramType = "form", dataType = "Integer", name = "userId", value = "用户编号", required = true)})
    public ResultData likeUserComment(Long commentId, Integer userId) throws Exception {
        if (!NumberHelper.validatePositive(commentId) || !NumberHelper.validatePositive(userId)) {
            throw new BizException(Meta.CODE_451.getCode(), "评论id,用户编号参数不正确");
        }
        baseService.likeUserComment(commentId, userId);
        return ResultData.newInstance();
    }

}
